package com.yunqian.util.wechat.bean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zou
 * @describe 微信小程序消息模板内容
 * @date 2018/7/17
 */
public class WxMpTemplateMessage {
    private String touser;
    private String template_id;
    private String page;
    private String form_id;
    private String emphasis_keyword;
    private List<WxMpTemplateData> data = new ArrayList<WxMpTemplateData>();
    private Map<String, String> params;


    public WxMpTemplateMessage() {
    }

    public WxMpTemplateMessage(String touser, String template_id, String page, String form_id) {
        this.touser = touser;
        this.template_id = template_id;
        this.page = page;
        this.form_id = form_id;
    }

    public WxMpTemplateMessage(String touser, String template_id, String page, String form_id, List<WxMpTemplateData> data) {
        this.touser = touser;
        this.template_id = template_id;
        this.page = page;
        this.form_id = form_id;
        this.data = data;
    }

    public WxMpTemplateMessage(String touser, String template_id, String page, String form_id, Map<String, String> params) {
        this.touser = touser;
        this.template_id = template_id;
        this.page = page;
        this.form_id = form_id;
        this.emphasis_keyword = emphasis_keyword;
        this.params = params;
    }

    public String getTouser() {
        return touser;
    }

    public void setTouser(String touser) {
        this.touser = touser;
    }

    public String getTemplate_id() {
        return template_id;
    }

    public void setTemplate_id(String template_id) {
        this.template_id = template_id;
    }

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    public String getForm_id() {
        return form_id;
    }

    public void setForm_id(String form_id) {
        this.form_id = form_id;
    }

    public String getEmphasis_keyword() {
        return emphasis_keyword;
    }

    public void setEmphasis_keyword(String emphasis_keyword) {
        this.emphasis_keyword = emphasis_keyword;
    }

    public List<WxMpTemplateData> getData() {
        return data;
    }

    public Map<String, String> getParams() {
        return params;
    }

    public void setParams(Map<String, String> params) {
        this.params = params;
    }

    public void setData(List<WxMpTemplateData> data) {
        this.data = data;
    }
    public String toJson() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("{");
        buffer.append(String.format("\"touser\":\"%s\"", this.touser)).append(",");
        buffer.append(String.format("\"template_id\":\"%s\"", this.template_id)).append(",");
        buffer.append(String.format("\"page\":\"%s\"", this.page)).append(",");
        buffer.append(String.format("\"form_id\":\"%s\"", this.form_id)).append(",");
        buffer.append("\"data\":{");
        WxMpTemplateData param = null;
        for (int i = 0; i < this.data.size(); i++) {
            param = data.get(i);
            // 判断是否追加逗号
            if (i < this.data.size() - 1) {

                buffer.append(String.format("\"%s\": {\"value\":\"%s\",\"color\":\"%s\"},", param.getName(), param.getValue(), param.getColor()));
            } else {
                buffer.append(String.format("\"%s\": {\"value\":\"%s\",\"color\":\"%s\"}", param.getName(), param.getValue(), param.getColor()));
            }

        }
        buffer.append("}");
        buffer.append("}");
        return buffer.toString();
    }

    public String paramToJson() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("{");
        buffer.append(String.format("\"touser\":\"%s\"", this.touser)).append(",");
        buffer.append(String.format("\"template_id\":\"%s\"", this.template_id)).append(",");
        buffer.append(String.format("\"page\":\"%s\"", this.page)).append(",");
        buffer.append(String.format("\"form_id\":\"%s\"", this.form_id)).append(",");
        buffer.append("\"data\":{");
        for (String key: params.keySet()) {
            buffer.append(String.format("\"%s\": {\"value\":\"%s\"},", key, params.get(key)));
        }
        //去除最后一个逗号
        buffer.deleteCharAt(buffer.length()-1);
        buffer.append("}");
        buffer.append("}");
        return buffer.toString();
    }
}
